/*
* SMILParser
*
* Version 1.0
*
* Programmers: Jesse Louderback, Noah Pham, Chad Carpenter, and Marc Hindman
*/
package MessageView;

import java.util.Timer;
import java.util.TimerTask;

import com.SmilDial.R;

import Message.SMILMessage;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

public class MessagePlayer extends View implements OnTouchListener {
	Paint paint = new Paint();
	private SMILMessage message = null;
	private int width;
	private int height;
	Timer time = new Timer();
	static float t = 0;
	private boolean running = true;
	/*
	 * Edit variables
	 */
	private boolean editMessage = false;
	private boolean sizing = false;
	private float moveX = -1;
	private float moveY = -1;
	private float distance = -1;
	float oldx1;
	float oldx2;
	float oldy1;
	float oldy2;

	// Constructor for view
	public MessagePlayer(Context context, SMILMessage mes) {
		super(context);
		t = 0;
		setFocusable(true);
		setFocusableInTouchMode(true);
		this.setOnTouchListener(this);
		paint.setAntiAlias(true);
		this.message = mes;
		time.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				if (running) {
					t += 1;
					postInvalidate();
				}
			}
		}, 500, 1000);
	}

	public MessagePlayer(Context context, SMILMessage mes, boolean edit) {
		super(context);
		setFocusable(true);
		setFocusableInTouchMode(true);
		this.setOnTouchListener(this);
		paint.setAntiAlias(true);
		this.message = mes;
		editMessage = edit;
		message.setEdit(edit);
		t = 0;
	}

	// draw the stuff
	@Override
	public void onDraw(Canvas canvas) {
		if (!message.isFinished(t)) {
			message.onDraw(canvas, t, paint);
		} else {
			message.stopMessage(canvas, paint);
		}
	}

	// setter
	public void setDisplaySize(int height, int width) {
		this.width = width;
		this.height = height;
	}

	public void restartMessage() {
		t = 0;
		message.reset();
	}

	public void pauseMessage() {
		running = false;
		message.pauseAudio();
	}

	public void resumeMessage() {
		running = true;
	}

	public void setFin(Bitmap r){
		message.setFin(r);
	}
	public void destroy() {
		time.cancel();
		time = null;
		message.destroy();
	}

	public boolean onTouch(View v, MotionEvent event) {
		if (editMessage) {
			if (event.getPointerCount() == 2) {
				sizing = true;
				float x1 = event.getX(0);
				float x2 = event.getX(1);
				float y1 = event.getY(0);
				float y2 = event.getY(1);
				if (distance == -1) {
					distance = (float) Math.sqrt((x1 - x2) * (x1 - x2)
							+ (y1 - y2) * (y1 - y2));
					oldx1 = x1;
					oldx2 = x2;
					oldy1 = y1;
					oldy2 = y2;
				} else {
					float distance2 = (float) Math.sqrt((x1 - x2) * (x1 - x2)
							+ (y1 - y2) * (y1 - y2));

					if (message.getItemToEdit() == 0) { // image

						float deltaX = ((x2 - oldx2) + (x1 - oldx1));
						float deltaY = ((y2 - oldy2) + (y1 - oldy1));
						message.sizeImgItem(deltaX, deltaY);
						oldx1 = x1;
						oldx2 = x2;
						oldy1 = y1;
						oldy2 = y2;
					} else if (message.getItemToEdit() == 1) { // text
						message.sizeItem("text", distance2 - distance);
					}
				}

			} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
				moveX = event.getX();
				moveY = event.getY();
			} else if (event.getAction() == MotionEvent.ACTION_UP
					&& event.getPointerCount() == 1) {
				distance = -1;
				sizing = false;
				oldx1 = oldx2 = oldy1 = oldy2 = 0;
			} else {
				if (sizing == false) {
					if (message.getItemToEdit() == 0) { // image
						message.moveItem("image", event.getX() - moveX, event.getY() - moveY);
					} else if (message.getItemToEdit() == 1) { // text
						message.moveItem("text", event.getX() - moveX, event.getY() - moveY);
					}
					moveX = event.getX();
					moveY = event.getY();
				}
			}
			this.invalidate();
		}
		return true;
	}

	public void setObject(String obj) {
		if (obj.equals("img")) {
			message.setItemToEdit("img");
		} else if (obj.equals("text")) {
			message.setItemToEdit("text");
		}
	}
}
